package com.example.drss.adapter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.example.drss.RegisterActivity;

import android.os.AsyncTask;

public class RegisterAsyncTask extends AsyncTask<String , Void ,String> {

	RegisterActivity activity;
	List<NameValuePair> params;
	
	public RegisterAsyncTask(RegisterActivity activity,List<NameValuePair> params){
		this.activity=activity;
		this.params=params;
	}

	@Override
	protected String doInBackground(String... parmas) {//可变参数
		// TODO Auto-generated method stub
		if(parmas.length>0){
			String url = parmas[0];

			String result = null;
			HttpPost httpPost = new HttpPost(url);
			HttpClient httpClient = new DefaultHttpClient();
			try {
				httpPost.setEntity(new UrlEncodedFormEntity(this.params ,HTTP.UTF_8));
				HttpResponse response = httpClient.execute(httpPost);
				int sc = response.getStatusLine().getStatusCode();
				if(HttpStatus.SC_OK == sc){
				HttpEntity entity = response.getEntity(); 
				return result = EntityUtils.toString(entity);
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			
		}
		//登录
		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		activity.onResponseLogin(result);
	}

}
